﻿using BUS;
using DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBV_Nhom8
{
    public partial class frmTraCuuBenhNhan : Form
    {
        public frmTraCuuBenhNhan()
        {
            InitializeComponent();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            string sql = string.Format(@"select * from HoSoBenhNhan where HoTen like '%{0}%' or MaHSBN = '{0}'", txtSearch.Text);
            dataGridView1.DataSource = HoSoBenhNhanBUS.TraCuuBenhNhan(sql);
            DinhDangHoSoBenhNhan();
        }

        private void frmTraCuuBenhNhan_Load(object sender, EventArgs e)
        {

        }


        private void DinhDangHoSoBenhNhan()
        {
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[1].HeaderText = "Mã Hồ Sơ";
            dataGridView1.Columns[2].HeaderText = "Họ Tên";
            dataGridView1.Columns[3].HeaderText = "Ngày Sinh";
            dataGridView1.Columns[4].HeaderText = "Giới Tính";
            dataGridView1.Columns[5].Visible = false;
            dataGridView1.Columns[6].Visible = false;
            dataGridView1.Columns[7].Visible = false;
            dataGridView1.Columns[8].Visible = false;
            dataGridView1.Columns[9].Visible = false;
            dataGridView1.Columns[10].Visible = false;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int maHoSo = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
                HoSoBenhNhanDTO hoso = HoSoBenhNhanBUS.LayThongTinHoSoBenhNhanTheoHoSoBenhNhan(maHoSo);
                txtMaHoSoBenhNhan.Text = hoso.MaHoSoBenhNhan.ToString();
                txtMaHS.Text = hoso.MaHSBN;
                txtHoTen.Text = hoso.HoTen;
                txtNgaySinh.Text = hoso.NgaySinh.ToString("dd/MM/yyyy");
                txtGioiTinh.Text = hoso.GioiTinh;
                txtCMND.Text = hoso.CMND;
                txtDiaChi.Text = hoso.DiaChi;
                txtDienThoai.Text = hoso.DienThoai;
                txtSoBaoHiem.Text = hoso.SoTheBaoHiem;
                txtNgayHetHan.Text = hoso.NgayHetHan.ToString("dd/MM/yyyy");
            }
        }

        private void btnLapHoSoBenhNhan_Click(object sender, EventArgs e)
        {
            frmHoSoBenhNhan obj = new frmHoSoBenhNhan();
            obj.ShowDialog();
        }

        private void btnLapPhieuKhamBenh_Click(object sender, EventArgs e)
        {
            try
            {
                int maHSBN = Convert.ToInt32(txtMaHoSoBenhNhan.Text);
                if (PhieuKhamBenhBUS.KiemTraPhieuKhamBenhTrongNgay(maHSBN).Rows.Count == 0)
                {
                    if (!string.IsNullOrEmpty(txtMaHoSoBenhNhan.Text))
                    {
                        PhieuKhamBenhDTO obj = new PhieuKhamBenhDTO();
                        obj.MaHoSoBenhNhan = maHSBN;
                        obj.NgayKham = DateTime.Now;
                        obj.NhanVienPhuTrach = "";
                        obj.ThanhToanMacDinh = 0;
                        int ma = PhieuKhamBenhBUS.ThemPhieuKhamBenh(obj);

                        MessageBox.Show("Lập phiếu khám bệnh thành công.\n Mã phiếu khám là: PKB" + ma);

                    }
                }
                else
                {
                    MessageBox.Show("Đã lập phiếu khám trong ngày hôm nay.");
                }
            }
            catch
            {
                MessageBox.Show("Lập phiếu khám thất bại.");
            }
        }
    }
}
